package com.htthorizon.system.service.impl;

import com.htthorizon.common.redis.service.RedisService;
import com.htthorizon.system.listener.StartupTimeListener;
import com.htthorizon.system.mapper.SysUserMapper;
import com.htthorizon.system.monitor.*;
import com.htthorizon.system.service.IMonitorService;
import com.htthorizon.system.service.ISysUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author Brilliant
 */
@Service
public class MonitorServiceImpl implements IMonitorService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private StartupTimeListener startupTimeListener;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public BusinessSystemOnlineNum getBusinessSystemOnlineNum() {
        Collection<String> keys = redisService.keys("login_token*");
        BusinessSystemOnlineNum businessSystemOnlineNum = new BusinessSystemOnlineNum();
        businessSystemOnlineNum.setBusinessSystemOnlineNum(keys.size());
        return businessSystemOnlineNum;
    }

    @Override
    public BusinessUserRegNum getBusinessUserRegNum() {
        Integer count =  sysUserService.countAll();
        BusinessUserRegNum businessUserRegNum = new BusinessUserRegNum();
        businessUserRegNum.setBusinessUserRegNum(count);
        return businessUserRegNum;
    }

    @Override
    public BusinessVisitCount getBusinessVisitCount() {
        Integer userNumAll = redisService.getCacheObject("UserNumAll");
        BusinessVisitCount businessVisitCount = new BusinessVisitCount();
        if (Objects.isNull(userNumAll)){
            businessVisitCount.setBusinessVisitCount(0);
            return businessVisitCount;
        }
        businessVisitCount.setBusinessVisitCount(userNumAll);
        return businessVisitCount;
    }

    @Override
    public BusinessSystemResponseTime getBusinessSystemResponseTime() {
        Long userNumAll = redisService.getCacheObject("BusinessSystemResponseTime");
        BusinessSystemResponseTime businessSystemResponseTime = new BusinessSystemResponseTime();
        if (Objects.isNull(userNumAll)){
            businessSystemResponseTime.setBusinessSystemResponseTime(3L);
            return businessSystemResponseTime;
        }
        businessSystemResponseTime.setBusinessSystemResponseTime(userNumAll);
        return businessSystemResponseTime;
    }

    @Override
    public BusinessSystemDBTime getBusinessSystemDBTime() {
        Long userNumAll = redisService.getCacheObject("BusinessSystemDBTime");
        BusinessSystemDBTime businessSystemResponseTime = new BusinessSystemDBTime();
        if (Objects.isNull(userNumAll)){
            businessSystemResponseTime.setBusinessSystemDBTime(3L);
            return businessSystemResponseTime;
        }
        businessSystemResponseTime.setBusinessSystemDBTime(userNumAll);
        return businessSystemResponseTime;

    }

    @Override
    public BusinessSystemRunningTime getBusinessSystemRunningTime() {
        long startupTime = startupTimeListener.getStartupTime();
        BusinessSystemRunningTime businessSystemRunningTime = new BusinessSystemRunningTime();
        businessSystemRunningTime.setBusinessSystemRunningTime(((System.currentTimeMillis()-startupTime)/1000));
        return businessSystemRunningTime;
    }
    @Value("${database}")
    private String database;

    @Override
    public BusinessDataTableSpace getBusinessDataTableSpace() {
      List<String> list=   sysUserMapper.getAllTable(database);
      double size=0;

      for (String tableName : list) {
         double size1=   sysUserMapper.getTableSize(tableName,database);
         size=size+size1;
      }
         size=size/1024D;
        BusinessDataTableSpace businessDataTableSpace = new BusinessDataTableSpace();
//        businessDataTableSpace.setBusinessDataTableSpace(size);
        businessDataTableSpace.setBusinessDataTableSpace(size);
        return businessDataTableSpace;

    }

    @Override
    public BusinessDayLoginNum getBusinessDayLoginNum() {

        Integer userNumAll = redisService.getCacheObject("userNumToDay");
        BusinessDayLoginNum businessSystemResponseTime = new BusinessDayLoginNum();
        if (Objects.isNull(userNumAll)){
            businessSystemResponseTime.setBusinessDayLoginNum(3);
            return businessSystemResponseTime;
        }
        businessSystemResponseTime.setBusinessDayLoginNum(userNumAll);
        return businessSystemResponseTime;
    }


}
